package com.web.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.biz.TUinfoUtypeBIZ;
import com.biz.TUserinfoBIZ;
import com.entity.TUinfoUtype;
import com.entity.TUserinfo;
import com.util.CookieUtil;
import com.util.ReturnValue;

@SuppressWarnings("rawtypes")
@Controller
public class TUserinfoController {
	
	private TUserinfoBIZ tUserinfoBIZ;
	@Resource
	public void settUserinfoBIZ(TUserinfoBIZ tUserinfoBIZ) {
		this.tUserinfoBIZ = tUserinfoBIZ;
	}
	
	private TUinfoUtypeBIZ tUinfoUtypeBIZ;
	@Resource
	public void settUinfoUtypeBIZ(TUinfoUtypeBIZ tUinfoUtypeBIZ) {
		this.tUinfoUtypeBIZ = tUinfoUtypeBIZ;
	}
	
//	private TUsertypeBIZ tUsertypeBIZ;
//	@Resource
//	public void settUsertypeBIZ(TUsertypeBIZ tUsertypeBIZ) {
//		this.tUsertypeBIZ = tUsertypeBIZ;
//	}

	
	/**
	 * 登陆
	 * 
	 */
	@RequestMapping("doLogin.do")
	public void doLogin(HttpServletRequest request,HttpServletResponse response) throws Exception{
	
		String userName = request.getParameter("userName");
		String userPassWord = request.getParameter("userPassWord");
		String randStr = request.getParameter("rand");
		Cookie rand =CookieUtil.getCookieByName(request, "rand");
		if(rand==null){
			ReturnValue.returnBySuccessOrFail(response, false,"验证码不存在！");
		}else{
			if(!rand.getValue().equals(randStr.trim())){
				System.out.println("cookie:::"+rand.getValue());
				ReturnValue.returnBySuccessOrFail(response, false,"输入的验证码不正确！");
			}else{
				TUserinfo temp = new TUserinfo();
				temp.setMobile(userName);
				temp.setName(userName);
				temp.setNickname(userName);
				temp.setPsw(userPassWord);
				TUserinfo tempUser = this.tUserinfoBIZ.doLogin(temp);
				if(tempUser!=null){
					int maxAge = 86400;
					List titList = this.tUinfoUtypeBIZ.findByUinfoId(tempUser.getId());
					if(titList.size()>0){
						TUinfoUtype tit =(TUinfoUtype) titList.get(0);
						CookieUtil.addCookie(response, "user_type_id", tit.getUtypeId(), maxAge);
					}
					request.getSession().setAttribute("user_id", tempUser.getId().toString());
					CookieUtil.addCookie(response, "user_nick_name", tempUser.getNickname().toString(), maxAge);
					ReturnValue.returnBySuccessOrFail(response, true,"登陆成功！");
				}else{
					ReturnValue.returnBySuccessOrFail(response, false,"用户名或密码错误！");
				}
			}
		}
	
	}
	
	@RequestMapping("login/doAddUserInfo.do")
	public void doAddUserInfo(HttpServletRequest request,HttpServletResponse response) throws IOException{
		
	}
	
	
}
